package com.bite.demo.controller;

import com.bite.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/param1")
    public String param1(String userName){
        return "接收到参数" + userName;
    }
    @RequestMapping("/param2")
    public String param2(Integer id){
        return "接收到参数" + id;
    }
    @RequestMapping("/param3")
    public String param3(int id){
        return "接收到参数" + id;
    }
    @RequestMapping("/param4")
    public String param4(UserInfo userInfo){
        return "接收到参数" + userInfo;
    }
    @RequestMapping("/param5")
    public String param5(@RequestParam(value = "userName", required = false) String name, Integer id){
        return "接收到参数" + name + id;
    }
    @RequestMapping("/param6")
    public String param6(String[] arrParams){
        return "接收到参数" + Arrays.toString(arrParams);
    }
    @RequestMapping("/param7")
    //默认传多个餐厨封装到数组, 需要加上@RequestParam映射到集合中
    public String param7(@RequestParam("listParams") List<String> listParams){
        return "接收到参数" + listParams;
    }
    @RequestMapping("/param8")
    //JSON数据映射到对象中使用@RequestBody
    public String param8(@RequestBody UserInfo userInfo){
        return "接收到参数" + userInfo;
    }
    @RequestMapping("/param9/{articleId}")//用大括号括起来的就是要作为参数传递的值
    //@PathVariable 翻译:路径变量 获取URL中的路径articleId作为参数传递
    public String param9(@PathVariable("articleId") Integer articleId){
        return "接收到参数" + articleId;
    }
    @RequestMapping("/param10/{id}/{name}")
    public String param10(@PathVariable("id") Integer id, @PathVariable("name") String userName){
        return "接收到参数" + id + userName;
    }
    @RequestMapping("/param11")
    //MultipartFile是Spring框架提供的一个接口，用于处理HTTP请求中的文件上传
    //@RequestPart方法 映射文件
    public String param11(@RequestPart("file1") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        //MultipartFile提供transferTo方法, 将MultipartFile转成File
        file.transferTo(new File("D:/temp/" + file.getOriginalFilename()));
        return "接收到参数" + file.getOriginalFilename();
    }

    @RequestMapping("/getCookie")
    //HttpServletRequest 这个类可以获取到请求中的所有东西
    public String getCookie(HttpServletRequest request){
        //.getCookies, 可以获取到所有的cookie值
        Cookie[] cookies = request.getCookies();
        System.out.println("获取Cookie信息:");
        if(cookies != null){
            for (Cookie cookie:
                 cookies) {
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "获取Cookie成功";

    }

    @RequestMapping("/getCookie2")
    //@CookieValue 只能获取到其中一个cookie, 有局限性
    public String getCookie2(@CookieValue("bite") String bite){
        System.out.println("从Cookie中获取bite:" + bite);
        return "获取Cookie成功";
    }
    //创建Session, 并返回cookieId给客户端
    //服务器重启, Session就会消失, Cookie中存储的SessionId就会失效
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //参数为true 如果Session对象不存在, 则创建一个session, 如果存在, 则直接返回
        HttpSession session = request.getSession(true);
        //attribute 属性
        session.setAttribute("name", "张三");
        session.setAttribute("studentId", "202211");
        return "设置session成功";
    }
    @RequestMapping("/setSession1")
    public String setSession1(HttpSession session){
        //attribute 属性
        session.setAttribute("name", "张三");
        session.setAttribute("studentId", "202211");
        return "设置session成功";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        //参数为false, 不存在session, 返回null
        HttpSession session = request.getSession(false);
        if(session != null){
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("studentId"));
        }
        System.out.println("null");
        return "获取Session成功";
    }

    @RequestMapping("/getSession2")
    //可直接用HttpSession
    public String getSession2(HttpSession session){
        System.out.println("name" + session.getAttribute("name"));
        System.out.println("studentId" + session.getAttribute("studentId"));
        return "获取Session成功";
    }
    @RequestMapping("/getSession3")
    //@SessionAttribute 只能获取到一个属性
    public String getSession3(@SessionAttribute(value = "name",required = false) String name){
        System.out.println("name" + name);
        return "获取Session成功";
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        //agent 代理
        String userAgent = request.getHeader("User-Agent");
        return "userAgent:" + userAgent;
    }
    @RequestMapping("/getHeader2")
    //@SessionAttribute 只能获取到一个属性
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        System.out.println("userAgent" + userAgent);
        return "userAgent:" + userAgent;
    }




}
